package com.atldy.commerce.service.hystrix;

import cn.hutool.json.JSONUtil;
import com.atldy.commerce.service.OpenfeignTokenService;
import com.atldy.commerce.vo.JwtToken;
import com.atldy.commerce.vo.UsernameAndPassword;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @Author: li-dong-yang
 * @CreateTime: 2021-09-13 7:22
 * @Description： openFeign集成Hystrix  ，兜底方法
 */
@Slf4j
@Component("openfeignTokenServiceFallbackFactory")
public class OpenfeignTokenServiceFallbackFactory implements FallbackFactory<OpenfeignTokenService> {


    @Override
    public OpenfeignTokenService create(Throwable throwable) {
        log.error("get exception  info [{},{}]", throwable.getMessage(), throwable);
        return new OpenfeignTokenService() {
            @Override
            public JwtToken getOpenfeignToken(UsernameAndPassword usernameAndPassword) {
                log.info("get request param UsernameAndPassword info [{}]", JSONUtil.toJsonStr(usernameAndPassword));
                return JwtToken.builder().token(throwable.getMessage()).build();
            }
        };
    }
}